package net.cardgame.orcalecard;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jp.jma.oraclecard.MyApplication;
import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.SelectedHistoryListener;
import net.cardgame.orcalecard.bean.CardBean;
import net.cardgame.orcalecard.bean.HistoryBean;
import net.cardgame.orcalecard.pref.SecurePreferences;
import net.cardgame.orcalecard.utils.ConstantValue;
import net.cardgame.orcalecard.utils.Sort;
import uk.co.senab.bitmapcache.BitmapLruCache;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HistoryActivity extends Activity implements
		SelectedHistoryListener, OnClickListener {
	ArrayList<HistoryBean> listHistory = new ArrayList<HistoryBean>();
	HistoryAdapter adapter;
	ArrayList<CardBean> listCardBean = new ArrayList<CardBean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_activity);
		ListView listView = (ListView) findViewById(R.id.listView_history);
		findViewById(R.id.btnhelp_history).setOnClickListener(this);
		findViewById(R.id.btnBack_history).setOnClickListener(this);
		getListHistory();
		if (!listHistory.isEmpty()) {
			adapter = new HistoryAdapter(this, listHistory);
			listView.setAdapter(adapter);
		}
	}

	void getListHistory() {
		Gson gson = new Gson();
		Type listOfHistoryBean = new TypeToken<List<HistoryBean>>() {
		}.getType();
		SecurePreferences appPreferences = new SecurePreferences(this,
				ConstantValue.APP_PREFERENCES,
				ConstantValue.PREFERENCES_SECRET_KEY, true);
		if (appPreferences.containsKey(ConstantValue.HISTORY_KEY)) {
			String strHistory = appPreferences
					.getString(ConstantValue.HISTORY_KEY);
			listHistory = gson.fromJson(strHistory, listOfHistoryBean);
			listHistory = Sort.sortHistoryByDate(listHistory);
		}
	}

	@Override
	public void onSelectedItemListener(int id) {
		// TODO Auto-generated method stub
		for (HistoryBean historyBean : listHistory) {
			if (historyBean.id == id)
				for (CardBean card : historyBean.cardBeans) {
					listCardBean.add(card);
				}
		}
		Intent intent = new Intent(this, EditHistoryActivity.class);
		intent.putExtra("historyId", id);
		startActivityForResult(intent, 0);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
	}

	@Override
	public void onDeleteHistory(HistoryBean history) {
		// TODO Auto-generated method stub
		listHistory.remove(history);
		SecurePreferences appPreferences = new SecurePreferences(this,
				ConstantValue.APP_PREFERENCES,
				ConstantValue.PREFERENCES_SECRET_KEY, true);
		Gson gson = new Gson();
		Type listOfHistoryBean = new TypeToken<List<HistoryBean>>() {
		}.getType();
		ArrayList<HistoryBean> listSave = listHistory;
		Collections.reverse(listSave);
		String save = gson.toJson(listSave, listOfHistoryBean);
		appPreferences.put(ConstantValue.HISTORY_KEY, save);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		findViewById(R.id.btnhelp_history).setOnClickListener(this);
		findViewById(R.id.btnBack_history).setOnClickListener(this);
		switch (v.getId()) {
		case R.id.btnhelp_history:
			Intent intent = new Intent(this, HelpActivity.class);
			intent.putExtra(HelpActivity.KEY_HELP_INDEX, 1);
			startActivityForResult(intent, 0);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_left);
			break;
		case R.id.btnBack_history:
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	private void clearCache() {
		BitmapLruCache cache = MyApplication.getInstance().getBitmapCache();
		for (CardBean card : listCardBean) {
			cache.removeFromMemory(ConstantValue.getPathCardDetail(this,
					card.deckId, card.cardId, ConstantValue.X3));
			cache.removeFromMemory(ConstantValue.getPathCardDetail(this,
					card.deckId, card.cardId, ConstantValue.X1));
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		clearCache();
		super.onDestroy();
	}

	@Override
	public void onFinished() {
		// TODO Auto-generated method stub

	}
}
